package com.yizhu.zczy.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yizhu.zczy.common.utils.PageUtils;
import com.yizhu.zczy.common.utils.R;
import com.yizhu.zczy.system.entity.MenuEntity;
import com.yizhu.zczy.system.vo.ShowAdminVo;
import com.yizhu.zczy.system.vo.UserNameAndPassWordVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yizhu.zczy.system.entity.UserEntity;
import com.yizhu.zczy.system.service.UserService;

import javax.validation.Valid;


/**
 * 后台管理员用户表
 *
 * @author guoyz
 * @email guoyz@mail.com
 * @date 2021-06-24 18:31:46
 */
@RestController
@RequestMapping("system/user")
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping("/list")
    @ApiOperation("查询管理员列表")
    public R list(@RequestParam(required = false ,defaultValue = "1") Long page ,@RequestParam(required = false ,defaultValue = "10")Long limit){
       IPage<ShowAdminVo> params=userService.queryPage(page,limit);
        return R.ok().put("page", params);
    }

    @PostMapping("/verifyAccountPassword")
    @ApiOperation("验证账号密码")
   public R verifyAccountPassword(@Valid UserNameAndPassWordVo userNameAndPassWordVo){
        try {
            userService.verifyAccountPassword(userNameAndPassWordVo);
            return R.ok();
        }catch (Exception e){
            return R.error(e.getMessage());
        }

    }

    @GetMapping("queryMenuByLoggedinUser")
    @ApiOperation("根据登录的用户查询菜单")
    public R queryMenuByLoggedinUser(){
        List<MenuEntity> menuEntities=userService.queryMenuByLoggedinUser();
        return R.ok().setData(menuEntities);

    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    //@RequiresPermissions("system:user:info")
    public R info(@PathVariable("userId") Integer userId){
		UserEntity user = userService.getById(userId);
        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("system:user:update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("system:user:delete")
    public R delete(@RequestBody Integer[] userIds){
		userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
